﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CapaEntidades;
using System.Data;
using System.Data.SqlClient;

namespace CapaDatos
{
    public class datReserva
    {
        #region singleton
        private static readonly datReserva _instancia = new datReserva();
        public static datReserva Instancia
        {
            get { return datReserva._instancia; }
        }
        #endregion

        #region Metodo
        public int RegistrarReserva(String cadXML)
        {
            SqlCommand cmd = null;
            try
            {
                SqlConnection conex = Conexion.Instancia.Conectar();
                cmd = new SqlCommand("spInsertarReserva", conex);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@prmcadXML", cadXML);
                conex.Open();
                int i = cmd.ExecuteNonQuery();
                return i;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                cmd.Connection.Close();
            }
        }
        public List<entReserva> ListarHistorialReservas(int idCliente)
        {
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            List<entReserva> Lista = null;
            try
            {
                SqlConnection conex = Conexion.Instancia.Conectar();
                cmd = new SqlCommand("spListarHistorialReserva", conex);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@prmIdPersona", idCliente);
                conex.Open();
                dr = cmd.ExecuteReader();
                Lista = new List<entReserva>();
                while (dr.Read())
                {
                    entReserva r = new entReserva();
                    r.IdReserva = Convert.ToInt32(dr["IDRESERVA"]);
                    r.FechaEntrada = Convert.ToDateTime(dr["FECHAENTRADA"]);
                    r.FechaSalida = Convert.ToDateTime(dr["FECHASALIDA"]);
                    r.MontoPagado = Convert.ToDouble(dr["MONTOPAGADO"]);
                    r.Saldo = Convert.ToDouble(dr["SALDO"]);
                    r.Total = Convert.ToDouble(dr["TOTAL"]);
                    Lista.Add(r);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cmd.Connection.Close();
            }
            return Lista;
        }
        public DataTable BuscarReservaPorID(int idReserva)
        {
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            DataTable dt = null;
            try
            {
                SqlConnection conex = Conexion.Instancia.Conectar();
                cmd = new SqlCommand("spBuscarReservaPorID", conex);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@prmID", idReserva);
                conex.Open();
                dr = cmd.ExecuteReader();
                dt = new DataTable();
                dt.Columns.Add("IdHabitacion", Type.GetType("System.Int32"));
                while (dr.Read())
                {
                    DataRow fila = dt.NewRow();
                    fila["IdHabitacion"] = dr["IDHABITACION"];
                    dt.Rows.Add(fila);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cmd.Connection.Close();
            }
            return dt;
        }
        #endregion
    }
}
